package com.example.admin.config;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云OSS配置类
 */
@Configuration
public class OSSConfig {
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

//    @Value("${aliyun.oss.urlPrefix}")
//    private String urlPrefix;

    @Bean
    public OSS ossClient() {
        // 创建ClientConfiguration实例，按需设置超时时间等参数
        ClientConfiguration configuration = new ClientConfiguration();
        // 设置连接超时为30秒
        configuration.setConnectionTimeout(30000);
        // 设置Socket超时为30秒
        configuration.setSocketTimeout(30000);
        // 设置请求超时重试次数
        configuration.setMaxErrorRetry(3);

        // 不使用configuration参数，因为当前版本可能不支持
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    public String getEndpoint() {
        return endpoint;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public String getBucketName() {
        return bucketName;
    }

//    public String getUrlPrefix() {
//        return urlPrefix;
//    }
}
